var util = require('../../utils/util.js');
const md = require('../../utils/md5.js');
const app = getApp();
const post = require('../../utils/post.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '2018-04-13',
    choose: 1,
    active: 1,
    pageindex: 1,
    pageszie: 20,
    srctype: "筛选",
    maintype: app.server.assettype
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var time = util.formatTime(new Date());
    var timearr = time.split(' ')[0];
    // 再通过setData更改Page()里面的data，动态更新页面的数据  
    this.setData({
      date: timearr
    });
    that.chushi();
    wx.getSystemInfo({
      success: function(res) {
        console.log(res.windowHeight);
        that.setData({
          scrollHeight: res.windowHeight - 80
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  chushi: function () {
    var that = this;
    // 获取时间戳
    var apinameIsMeb = "/BO/SA_BillVouch/Query";
    var timestamp = new Date().getTime();
    timestamp = parseInt(timestamp / 1000);
    var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
    var where = "DepID = " + wx.getStorageSync('depid') + " and BillDate >= '" + that.data.date + " 00:00:00' and BillDate <= '" + that.data.date + " 23:59:59' ";
    wx.request({
      //后台接口地址
      url: app.server.url + apinameIsMeb,
      data: {
        sign: md5IsMeb,
        appid: app.server.appid,
        tmsp: timestamp,
        pageindex: 1,
        pagesize: 20,
        summary: 1,
        where: where
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          var data = [];
          var shouru=0;
          var tuikuan =0;
          for (var i = 0; i < res.data.data.length; i++) {
            // data[i] = res.data.data[i];
            // data[i]['BillDate'] = res.data.data[i].AccDate.split('T')[0];
            // data[i]['BillDate'] = res.data.data[i].AccDate.split(' ')[0];
            if (res.data.data[i].MoneySum >0){
              shouru += res.data.data[i].MoneySum;
            }else{
              tuikuan += res.data.data[i].MoneySum;
            }
            
          }
          that.setData({
            dayinfo: res.data.data,
            moneyget: res.data.summary.MoneySum ? post.toDecimal2(res.data.summary.MoneySum) : 0,
            totalcount: res.data.totalcount,
            pageindex:1
          })
        } else {
          wx.showModal({
            title: '',
            content: res.data.msg,
          })
          return;
        }
      }
    })
  },
  bindDateChange: function (e) {
    var that = this;
    console.log('picker发送选择改变，携带值为', e.detail.value)
    that.setData({
      date: e.detail.value
    });

    // return;
    // 获取时间戳
    var date = that.data.date;
    var srctype = that.data.srctype;
    var where = "DepID = " + wx.getStorageSync('depid') + " and BillDate >= '" + e.detail.value + " 00:00:00' and BillDate <= '" + e.detail.value + " 23:59:59' ";
    // console.log(where);
    var apinameIsMeb = "/BO/SA_BillVouch/Query";
    var timestamp = new Date().getTime();
    timestamp = parseInt(timestamp / 1000);
    var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
    wx.request({
      //后台接口地址
      url: app.server.url + apinameIsMeb,
      data: {
        sign: md5IsMeb,
        appid: app.server.appid,
        tmsp: timestamp,
        pageindex: 1,
        pagesize: 20,
        summary: 1,
        where: where
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          var data = [];
          var shouru = 0;
          var tuikuan = 0;
          for (var i = 0; i < res.data.data.length; i++) {
            // data[i] = res.data.data[i];
            // data[i]['BillDate'] = res.data.data[i].AccDate.split('T')[0];
            // data[i]['BillDate'] = res.data.data[i].AccDate.split(' ')[0];
            if (res.data.data[i].MoneySum > 0) {
              shouru += res.data.data[i].MoneySum;
            } else {
              tuikuan += res.data.data[i].MoneySum;
            }
          }
          that.setData({
            dayinfo: res.data.data,
            moneyget: res.data.summary.MoneySum?post.toDecimal2(res.data.summary.MoneySum):0,
            totalcount: res.data.totalcount,
            pageindex: 1
          })
        } else {
          wx.showModal({
            title: '',
            content: res.data.msg,
          })
          return;
        }
      }
    })

  },
  newdata:function(){
    wx.showLoading({
      title: '正在加载...',
    })
    var that =this;
    var date = that.data.date;
    var srctype = that.data.srctype;
    var where = "DepID = " + wx.getStorageSync('depid') + " and BillDate >= '" + date + " 00:00:00' and BillDate <= '" + date + " 23:59:59' ";
    // console.log(where);
    var apinameIsMeb = "/BO/SA_BillVouch/Query";
    var timestamp = new Date().getTime();
    timestamp = parseInt(timestamp / 1000);
    var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
    var pageindex = that.data.pageindex + 1;
    console.log(pageindex);
    wx.request({
      //后台接口地址
      url: app.server.url + apinameIsMeb,
      data: {
        sign: md5IsMeb,
        appid: app.server.appid,
        tmsp: timestamp,
        pageindex: pageindex,
        pagesize: 20,
        summary: 1,
        where: where
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        wx.hideLoading();
        // console.log(res);
        if (res.data.code == 0) {
          var data = [];
          var shouru = 0;
          var tuikuan = 0;
          var dayinfo = that.data.dayinfo;
          for (var i = 0; i < res.data.data.length; i++) {
            if (res.data.data[i].MoneySum > 0) {
              shouru += res.data.data[i].MoneySum;
            } else {
              tuikuan += res.data.data[i].MoneySum;
            }
            dayinfo.push(res.data.data[i]);
          }
          that.setData({
            dayinfo: dayinfo,
            pageindex: pageindex
          })
        } else {
          wx.showModal({
            title: '',
            content: res.data.msg,
          })
          return;
        }
      }
    })
  },
  tobilldetail:function(e){
    var id = e.currentTarget.dataset.id;
    console.log(id);
    wx.navigateTo({
      url: '/pages/billdetail/billdetail?id=' + id+"&type=business",
    })
  }


})